/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import data.UserData;
import data.AdminData;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import database.UserDB;

/**
 *
 * @author Muhammad Dony
 */
public class TambahAnggotaAdminServlet extends HttpServlet {
    UserData listUser;
    UserDB userdb;
    HttpSession session;
    private String action;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    
    
    
    protected void halamanAnggota(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        session = request.getSession();
        AdminData admin = (AdminData) session.getAttribute("adminMasuk");
        List listdata = new UserDB().getdata();
        request.setAttribute("listdata", listdata);        
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/view_anggota.jsp");
        rd.forward(request, response);
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // login validation start
        session = request.getSession();
        Object ud = session.getAttribute("adminMasuk");
        AdminData udata = (AdminData) ud;
        if (udata != null) {
           // if (udata.isLogin()) { // if login
                action = request.getParameter("action");
                if (request.getParameter("action") == null) {
                    try {
                        halamanAnggota(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(TambahAnggotaAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(TambahAnggotaAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("add")) {
                    halamanTambahAnggota(request, response);
                } else if (action.equals("edit")) {
                    try {
                        halamanEditAnggota(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(TambahAnggotaAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(TambahAnggotaAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("delete")) {
                    try {
                        deleteAnggota(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(TambahAnggotaAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(TambahAnggotaAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                else { // if not login
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
                rd.forward(request, response);
            }
        } else {
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
            rd.forward(request, response);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // get session
        session = request.getSession();
        // mengambil object sesiAdmin dari session
        Object ud = session.getAttribute("adminMasuk");
        AdminData udata = (AdminData) ud;
        // mengambil object dengan parameter action dari request
        action = request.getParameter("action");

        if (request.getParameter("action") == null) {
                    try {
                        halamanAnggota(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(TambahAnggotaAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(TambahAnggotaAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("Tambah")) {
                    try {
                        //halamanEditAnggota(request, response);
                        tambahAnggota(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(TambahAnggotaAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(TambahAnggotaAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("Edit")) {
                    try {
                        //halamanEditAnggota(request, response);
                        editAnggota(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(TambahAnggotaAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(TambahAnggotaAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                
                }
    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    public void halamanTambahAnggota(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("title", "Tambah Anggota");
        request.setAttribute("buttonName", "Tambah");
        request.setAttribute("listUser", new UserData());
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/tambah_anggota.jsp");
        rd.forward(request, response);
    }

    public void halamanEditAnggota(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        request.setAttribute("title", "Edit Anggota");
        request.setAttribute("buttonName", "Edit");
        String ID  = request.getParameter("id");
        List listdata = new UserDB().getdataById(ID);
        UserData dataAnggota = (UserData) listdata.get(0);
        request.setAttribute("dataAnggota", dataAnggota);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/tambah_anggota.jsp");
        rd.forward(request, response);
    }

    public void tambahAnggota(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        // membuat object listUser
        listUser = new UserData();
        // set listUser
        listUser.setID(request.getParameter("ID"));
        listUser.setFirstname(request.getParameter("Firstname"));
        listUser.setLastname(request.getParameter("Lastname"));
        listUser.setEmail(request.getParameter("email"));
        listUser.setPassword(request.getParameter("password"));
        // memasukkannya dalam database
        userdb = new UserDB();
        userdb.insertData(listUser);
        // redirect ke halaman pelaporan
        halamanAnggota(request, response);
    }

    public void editAnggota(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        // membuat object listUser
        listUser = new UserData();
        // set listUser
        listUser.setID(request.getParameter("ID"));
        listUser.setFirstname(request.getParameter("Firstname"));
        listUser.setLastname(request.getParameter("Lastname"));
        listUser.setEmail(request.getParameter("email"));
        listUser.setPassword(request.getParameter("password"));
        // memasukkannya dalam database
        userdb = new UserDB();
        userdb.updateData(listUser);
        // redirect ke halaman pelaporan
        halamanAnggota(request, response);
    }

    public void deleteAnggota(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        String ID = request.getParameter("id");
        userdb = new UserDB();
        userdb.deleteData(ID);
        halamanAnggota(request, response);
    }
    
}
